package com.erp.dao.impl;

import com.erp.bean.Role;
import com.erp.bean.Supplier;
import com.erp.dao.SupplierdDao;
import com.erp.util.db.DataBaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author ld
 * 供应商业务数据实现类
 */
public class SupplierdDaoImpl extends DataBaseDao implements SupplierdDao {

    /**
     * 查询供应商列表
     */
    @Override
    public List<Supplier> findAll() {
        List<Supplier> list = new ArrayList<>();
        //1.创建SQL命令语句
        try {
            //获取数据库连接对象
            buildConnection();
            String sql = "select * from supplier";
            //2.创建预编译指令
            pstmt = conn.prepareStatement(sql);
            //3.执行预编译指令得到结果集
            rs = pstmt.executeQuery();
            while (rs.next()) {
                Supplier supplier = new Supplier(rs.getInt(1), rs.getString(2),
                        rs.getString(3), rs.getString(4), rs.getString(5));
                list.add(supplier);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return list;
    }

    /**
     * 更新供应商列表
     */
    @Override
    public int update(Supplier supplier) {
        int i = 0;
        //1.创建SQL语句命令
        try {
            //获取数据库连接对象
            buildConnection();
            String sql = " update supplier set supname=? ,tel=?,address=?,email=? where supid=?";
            //2.创建预编译指令
            pstmt = conn.prepareStatement(sql);
            //3.执行预编译指令+设置参数
            pstmt.setString(1, supplier.getSupname());
            pstmt.setString(2, supplier.getTel());
            pstmt.setString(3, supplier.getAddress());
            pstmt.setString(4, supplier.getEmail());
            pstmt.setInt(5, supplier.getSupid());
            //4.得到结果集
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return i;
    }

    /**
     * 删除供应商列表
     */
    @Override
    public int deldte(int supid) {
        int i = 0;
        //1.创建SQL命令语句
        try {
            //获取数据库连接对象
            buildConnection();
            String sql = "delete from supplier where supid=?";
            //2.创建预编译指令
            pstmt = conn.prepareStatement(sql);
            //3.执行预编译指令+设置参数
            pstmt.setInt(1, supid);
            //4.得到结果集
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return i;
    }

    /**
     * 添加供应商列表
     */
    @Override
    public int add(Supplier supplier) {
        int i = 0;
        //1.创建SQL命令语句
        try {
            //获取数据库连接对象
            buildConnection();
            String sql = "insert into supplier (supname,tel,address,email)values  (?,?,?,?)";
            //2.创建预编译指令
            pstmt = conn.prepareStatement(sql);
            //3.执行预编译指令+设置参数
            pstmt.setString(1, supplier.getSupname());
            pstmt.setString(2, supplier.getTel());
            pstmt.setString(3, supplier.getAddress());
            pstmt.setString(4, supplier.getEmail());
            //4.得到结果集
            i = pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return i;
    }

    /**
     * 根据id查询供应商
     */

    @Override
    public Supplier findBySupid(int supid) {
        Supplier supplier=null;
        try {
            //获取数据库连接对象
            buildConnection();
            //1.创建SQL命令语句
            String sql = "select * from supplier where supid=?";
            //2.创建预编译指令
            pstmt=conn.prepareStatement(sql);
            //设置参数
            pstmt.setInt(1,supid);
            //3.执行预编译指令得到结果集
            rs=pstmt.executeQuery();
            //4.得到结果集
            while(rs.next()){
                supplier=new Supplier(rs.getInt(1),rs.getString(2),
                        rs.getString(3),rs.getString(4),
                        rs.getString(5));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return supplier;
    }

}
